package auth

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"ruoyi/models"
	"ruoyi/models/auth"
	"ruoyi/utils/jwtutil"
	"ruoyi/utils/log"
)

func Login(c *gin.Context) {
	var user auth.UserInfo
	err := c.ShouldBindBodyWithJSON(&user)
	if err != nil {
		c.JSON(http.StatusOK, models.Error("数据解析失败"))
		return
	}
	log.Debug(map[string]any{"username": user.Username}, "登录系统")
	if user.Username == "admin" && user.Password == "123" {
		token := jwtutil.GenToken(user.Username)
		c.JSON(http.StatusOK, models.Success(token, "登录成功"))
		log.Debug(map[string]any{"username": user.Username}, "登录成功")
	} else {
		c.JSON(http.StatusOK, models.Error("用户名或密码错误"))
		log.Debug(map[string]any{"username": user.Username}, "登录失败")
	}
}

func Logout(c *gin.Context) {
	c.JSON(http.StatusOK, models.Success("", "退出成功"))
	log.Debug(nil, "用户已退出")
}

func IndexInfo(c *gin.Context) {
	c.JSON(http.StatusOK, models.Success("", "首页数据"))
}
